<template>
  <div class="register-container">
    <div class="background-wrapper">
      <div class="gradient-bg"></div>
      <div class="decorative-shapes">
        <div class="shape circle"></div>
        <div class="shape triangle"></div>
        <div class="shape wave"></div>
      </div>
    </div>
    
    <el-card class="register-box">
      <div class="branding">
        <div class="logo">
          <svg-icon icon-class="user-add" class="icon" />
        </div>
        <h2>创建管理员账号</h2>
        <p class="subtitle">请填写注册信息</p>
      </div>

      <el-form :model="form" :rules="rules" ref="registerForm">
        <el-form-item prop="username" label="用户名：" label-width="80px">
          <el-input 
            v-model="form.username" 
            placeholder="5-16位非空字符"
            class="custom-input"
          >
            <template #prefix>
              <svg-icon icon-class="user" class="input-icon" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password" label="密码：" label-width="80px">
          <el-input 
            v-model="form.password" 
            type="password" 
            placeholder="5-16位非空字符"
            class="custom-input"
            show-password
          >
            <template #prefix>
              <svg-icon icon-class="lock" class="input-icon" />
            </template>
          </el-input>
        </el-form-item>

        <div class="action-group">
          <el-button 
            type="primary" 
            class="register-btn"
            @click="handleRegister"
            :loading="loading"
          >
            <span v-if="!loading">立即注册</span>
            <template #loading>
              <span class="loading-text">注册中...</span>
            </template>
          </el-button>
          
          <el-button 
            class="back-btn"
            @click="$router.push('/login')"
          >
            <svg-icon icon-class="arrow-left" class="btn-icon" />
            返回登录
          </el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>
  
  <script setup>
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import request from '../utils/request'
  import { useTokenStore } from '../stores/token';
  import { register } from '@/api/admin';
  const router = useRouter()
  
  const form = ref({
    username: '',
    password: ''
  })
  
  const rules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { pattern: /^\S{5,16}$/, message: '用户名格式不正确', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { pattern: /^\S{5,16}$/, message: '密码格式不正确', trigger: 'blur' }
    ]
  }
  
  const loading = ref(false)
  
  const handleRegister = async () => {
    try {
      loading.value = true
      const res = await register(form.value)
      ElMessage.success(res.msg? res.msg : '注册成功，请登录')
      router.push('/admin/login')
    } catch (error) {
      ElMessage.error(error.message || '注册失败')
    } finally {
      loading.value = false
    }
  }
  </script>
  
  <style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
}

.background-wrapper {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.gradient-bg {
  position: absolute;
  width: 150%;
  height: 150%;
  background: linear-gradient(45deg, #4f46e5 0%, #818cf8 100%);
  transform: rotate(-15deg);
  opacity: 0.1;
}

.decorative-shapes {
  position: absolute;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  opacity: 0.1;
}

/* 形状样式与登录页保持一致 */

.register-box {
  width: 440px;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  position: relative;
  z-index: 1;
  transition: transform 0.3s ease;
}

.register-box:hover {
  transform: translateY(-5px);
}

.branding {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  background: #4f46e5;
  border-radius: 50%;
  margin: 0 auto 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 6px rgba(79, 70, 229, 0.1);
}

.icon {
  font-size: 40px;
  color: white;
}

h2 {
  font-size: 24px;
  color: #1f2937;
  margin-bottom: 8px;
}

.subtitle {
  color: #6b7280;
  font-size: 14px;
}

.custom-input {
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #4b5563;
}

.input-icon {
  font-size: 18px;
  color: #9ca3af;
  margin-right: 8px;
}

.action-group {
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.register-btn {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  background: linear-gradient(45deg, #4f46e5, #6366f1);
  border: none;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
}

.back-btn {
  width: 100%;
  border-color: #e5e7eb;
  color: #6b7280;
  transition: all 0.3s ease;
}

.back-btn:hover {
  color: #4f46e5;
  border-color: #c7d2fe;
  background-color: #eef2ff;
}

.btn-icon {
  font-size: 14px;
  margin-right: 6px;
}

.loading-text {
  letter-spacing: 1px;
}
</style>